package com.example.batchprocessing.test;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.*;
import org.springframework.batch.item.database.JdbcCursorItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;

@Configuration
@EnableBatchProcessing // 开启批处理 系统会在启动时执行，阻止自动执行job需要在配置文件添加配置
public class BatchConfiguration {

    /**
     * 作业仓库
     */
    //使用默认提供方式
    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    // tag::readerwriterprocessor[] 1.读数据
    @Bean
    public ItemReader<PeopleX> reader(DataSource dataSource) throws UnexpectedInputException, ParseException, Exception {
        //读数据可以读文件、数据库等，我这只使用了读取数据库的方式

        JdbcCursorItemReader<PeopleX> itemReader = new JdbcCursorItemReader<PeopleX>();

        itemReader.setDataSource(dataSource);
        itemReader.setSql("select * from people");
        itemReader.setRowMapper(new BeanPropertyRowMapper<PeopleX>(PeopleX.class));
        ExecutionContext executionContext = new ExecutionContext();
        itemReader.open(executionContext);
        Object customerCredit = new Object();
        while (customerCredit != null) {
            customerCredit = itemReader.read();
        }
        itemReader.close();
        return itemReader;
    }

    //2.处理数据
    @Bean
    public JdbcItemProcessor processor() {
        return new JdbcItemProcessor();
    }

    //3.写数据
    @Bean
    public JdbcItemWriter writer(JdbcTemplate jdbcTemplate) {
        //自定义写数据操作
        JdbcItemWriter writer = new JdbcItemWriter(jdbcTemplate);
        return writer;
    }

    // end::readerwriterprocessor[]

    // tag::jobstep[] 生成job
    @Bean
    public Job importJob(JobBuilderFactory jobs, Step s1, JdbcItemJobListener listener) {
        return jobs.get("importJob")
                .incrementer(new RunIdIncrementer())
                .listener(listener)
                .flow(s1)
                .end()
                .build();
    }

    //执行步骤
    @Bean
    public Step step1(StepBuilderFactory stepBuilderFactory, ItemReader<PeopleX> reader,
                      JdbcItemWriter jdbcItemWriter, ItemProcessor<PeopleX, DealBean> processor) {
        return stepBuilderFactory.get("step1")
                .<PeopleX, DealBean>chunk(100)//一次处理多少数据
                .reader(reader)
                .processor(processor)
                .writer(jdbcItemWriter)
                .build();
    }
}
